#include "structure.h"


MAP new_map(){
    NEW(ret,struct map_);
    for(int i=0;i<10;i++) ret->entry[i]=(List){.begin=NULL,.end=NULL};
    return ret;
}

void ** map_insert(MAP self, uLL key){
    int id=key % 10;
    FOR_LIST(now, self->entry[id]) 
    if(LIST_GET(now,map_unit)->key==key) return &(LIST_GET(now,map_unit)->pointer);
    NEW_V(now,struct map_unit_,.key=key,.pointer=NULL)
    LIST_INSERT(self->entry[id],now);
    return &(now->pointer);
}

void ** map_find(MAP self, uLL key){
    int id=key % 10;
    FOR_LIST(now, self->entry[id]) 
    if(LIST_GET(now,map_unit)->key==key) return &(LIST_GET(now,map_unit)->pointer);
    return NULL;
}

void map_erase(MAP self, uLL key){
    int id=key%10;
    FOR_LIST(now,self->entry[id]) if(LIST_GET(now,map_unit)->key==key){
        DELETE(LIST_GET(now,map_unit));
        LIST_DELETE(self->entry[id],now);
        break;
    }
    return;
}

void map_clear(MAP self){
    list nxt=NULL;
    for(int i=0;i<10;i++)
    FOR_LIST_2(now,self->entry[i],nxt) {
        nxt=now->nxt;
        DELETE(LIST_GET(now,map_unit));
        LIST_DELETE(self->entry[i],now);
    }
}

void map_cpy(MAP x,MAP y){
    map_clear(y);
    FOR_MAP(iter,x)
        *map_insert(y,MAP_KEY(iter))=MAP_VAL(iter,void *);
    return;
}

void set_insert(MAP self,uLL key){
    *(map_insert(self,key))=(void *)1;
    return;
}

bool set_find(MAP self, uLL key){
    void ** temp=map_find(self,key);
    return temp&&*temp==(void *) 1; 
}
